/*
 * PACKAGE NAME: com.jodinfo.outmirror.api.widgetattr.service
 * FILE    NAME: WidgetAttrDaoImpl.java
 * COPYRIGHT: Copyright(c) © 2016 heroj Company Ltd. All Rights Reserved
 */ 
package com.jodinfo.outmirror.api.widgetattr.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.hero.core.constant.SysConstant;
import com.hero.core.entity.PageResult;
import com.hero.core.entity.ResponseEntity;
import com.hero.core.exception.AbstractGenericException;
import com.hero.common.web.excel.util.ExcelExport;
import com.hero.core.util.StringUtils;
import com.hero.core.util.ValidationUtil;
import com.jodinfo.outmirror.api.controlwidget.entity.ControlWidgetEntity;
import com.jodinfo.outmirror.api.controlwidget.service.ControlWidgetQueryService;
import com.jodinfo.outmirror.api.widgetattr.entity.WidgetAttrEntity;

@Service(value="widgetAttrBizService")
public class WidgetAttrBizServiceImpl implements WidgetAttrBizService {
    private static final Logger logger = LoggerFactory.getLogger(WidgetAttrBizServiceImpl.class);
 	
 	@Resource(name="widgetAttrDmlService")
	private WidgetAttrDmlService widgetAttrDmlService;
	@Resource(name="widgetAttrQueryService")
	private WidgetAttrQueryService widgetAttrQueryService;
	@Resource(name="controlWidgetQueryService")
	private ControlWidgetQueryService controlWidgetQueryService;
	
	/** 
	 * 
	 * @author shensu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.widgetattr.WidgetAttrBizService#addWidgetAttr(WidgetAttrEntity widgetAttrEntity)
	 * @param WidgetAttrEntity widgetAttrEntity
	 * @return int
	 */
	public ResponseEntity addWidgetAttr(WidgetAttrEntity widgetAttrEntity) {
			ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
			  	 int i =  widgetAttrDmlService.addWidgetAttr(widgetAttrEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(AbstractGenericException e){
				logger.error(e.getFullMessage());
				rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
			}catch(Exception e){
				String errorMessage = StringUtils.toString(e);
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author shensu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.widgetattr.WidgetAttrBizService#updateWidgetAttr(WidgetAttrEntity widgetAttrEntity)
	 * @param WidgetAttrEntity widgetAttrEntity
	 * @return ResponseEntity
	 */
	public ResponseEntity updateWidgetAttr(WidgetAttrEntity widgetAttrEntity) {
		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
				int i = widgetAttrDmlService.updateWidgetAttr(widgetAttrEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(AbstractGenericException e){
				logger.error(e.getFullMessage());
				rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
			}catch(Exception e){
				String errorMessage = StringUtils.toString(e);
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author shensu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.widgetattr.WidgetAttrBizService#queryAllWidgetAttr(WidgetAttrEntity widgetAttrEntity)
	 * @param WidgetAttrEntity widgetAttrEntity
	 * @return String
	 */
	public String queryAllWidgetAttr(WidgetAttrEntity widgetAttrEntity) {
		return JSON.toJSONString(widgetAttrQueryService.queryAllWidgetAttr(widgetAttrEntity));
	} 
	/** 
	 * 
	 * @author shensu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.widgetattr.WidgetAttrBizService#queryAllWidgetAttr(WidgetAttrEntity widgetAttrEntity)
	 * @param WidgetAttrEntity widgetAttrEntity
	 * @return PageResult
	 */
	public String queryListForPageWidgetAttr(WidgetAttrEntity widgetAttrEntity) {
		return new PageResult( widgetAttrQueryService.queryListForPageWidgetAttr(widgetAttrEntity), widgetAttrQueryService.queryCountWidgetAttr(widgetAttrEntity)).toString();
	} 
	/** 
	 * 
	 * @author shensu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.widgetattr.WidgetAttrQueryService#queryById(WidgetAttrEntity widgetAttrEntity)
	 * @param BigInteger id
	 * @return
	 */
	public WidgetAttrEntity queryById(BigInteger id) {
		return widgetAttrQueryService.queryById(id);
	}
	
	public void exportWidgetAttr(WidgetAttrEntity widgetAttrEntity, HttpServletResponse response) {
		List<WidgetAttrEntity> exportList = null;
		String ids = widgetAttrEntity.getIds();
		if (ValidationUtil.isNullOrEmptyString(ids)) {
			exportList =widgetAttrQueryService.queryAllWidgetAttr(widgetAttrEntity);
		} else {
			String[] idArray = ids.split(",");
			List<BigInteger> idList = new ArrayList<BigInteger>();
			for(String id : idArray) {
				idList.add(new BigInteger(id));
			}
			exportList = widgetAttrQueryService.queryByIds(idList);
			// 将字典中的code转换为name
		   //convertCodeToName(exportList);
		}
		ExcelExport.exportExcel(exportList, "WidgetAttrEntityExcel", "WidgetAttrEntity信息", response);
	}
	/** 
	 * 
	 * @author shensu
	 * @date 2017年3月16日 上午11:09:01
	 * @see com.jodinfo.outmirror.api.widgetattr.service.WidgetAttrBizService#queryAllWidgetAttrByControlId(java.lang.String)
	 * @param controlId
	 * @return
	 */
	public String queryAllWidgetAttrByControlId(String controlId) {
		List<ControlWidgetEntity> listControl = new ArrayList<ControlWidgetEntity>();
		ControlWidgetEntity controlWidgetEntity = new ControlWidgetEntity();
		List<ControlWidgetEntity> listAllControl = controlWidgetQueryService.queryAllControlWidget(controlWidgetEntity);
		listControl = buildByRecursive(controlId,listControl,listAllControl);
		List<WidgetAttrEntity> allAttrList = concateAllAttrList(listControl);
		return JSON.toJSONString(allAttrList);
	}
	/**
	 * 封装所有属性
	 * @author shensu
	 * @date 2017年3月16日 上午11:57:51
	 * @param listControl
	 * @return
	 */
	private List<WidgetAttrEntity> concateAllAttrList(List<ControlWidgetEntity> listControl) {
		List<WidgetAttrEntity> listAllAttr = new ArrayList<WidgetAttrEntity>(); 
		for(ControlWidgetEntity controlWidgetEntity:listControl){
			WidgetAttrEntity widgetAttrEntity = new WidgetAttrEntity();
			widgetAttrEntity.setSortField("id");
			widgetAttrEntity.setSortOrder(SysConstant.ASC);
			widgetAttrEntity.setControlId(controlWidgetEntity.getControlId());
			listAllAttr.addAll(widgetAttrQueryService.queryAllWidgetAttr(widgetAttrEntity));
		}
		   Collections.sort(listAllAttr, new Comparator<WidgetAttrEntity>() {
	            public int compare(WidgetAttrEntity user1, WidgetAttrEntity user2) {
	                BigInteger id1 = user1.getId();
	                BigInteger id2 = user2.getId();
	                                //可以按User对象的其他属性排序，只要属性支持compareTo方法
	                return id1.compareTo(id2);
	            }
	        });
		return listAllAttr;
	}
	/**
	 * 返回自己本身及所有父级控件(递归调用)
	 * @author shensu
	 * @date 2017年3月16日 上午11:25:53
	 * @param controlId
	 * @return
	 */
	private List<ControlWidgetEntity> buildByRecursive(String controlId,List<ControlWidgetEntity> listControl,List<ControlWidgetEntity> listAllControl) {
		for(ControlWidgetEntity  controlWidgetEntity:listAllControl){
			if(controlId.equals(controlWidgetEntity.getControlId())){
				listControl.add(controlWidgetEntity);
			}
		}
		if(!SysConstant.ZERO_STRING.equals(controlId)){
			controlId = listControl.get(listControl.size()-1).getParentControlId();
			buildByRecursive(controlId,listControl,listAllControl);
		}
		return listControl;
	}

	
}

